package com.th.control;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.th.bean.ShoppingCart;
import com.th.common.BaseContext;
import com.th.common.R;
import com.th.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> slw = new LambdaQueryWrapper<>();
        slw.eq(ShoppingCart::getUserId,currentId);
        Long dishId = shoppingCart.getDishId();

        if(dishId != null){
            slw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            slw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        ShoppingCart sc = shoppingCartService.getOne(slw);
        if(sc == null){
            shoppingCart.setUserId(currentId);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartService.save(shoppingCart);
        }else {
            Integer number = sc.getNumber();
            sc.setNumber(number+1);
            shoppingCartService.updateById(sc);
        }

        return R.success(sc);

    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> slw = new LambdaQueryWrapper<>();
        slw.eq(ShoppingCart::getUserId,currentId);
        slw.orderByDesc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(slw);

        return R.success(list);

    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());

        if (setmealId!=null){
            queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
        }else {
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        Integer number = one.getNumber();
        if(number==1){
            shoppingCartService.remove(queryWrapper);
        }else {
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }

        return R.success(one);
    }


    @DeleteMapping("/clean")
    public R<String> clean(){
        Long userid = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> slw = new LambdaQueryWrapper<>();
        slw.eq(ShoppingCart::getUserId,userid);

        shoppingCartService.remove(slw);
        return R.success("清空购物车");
    }
}
